const app = getApp();
var share_count = 0;
var api = require('../../api.js');
var mta = require('../../mta_analysis.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconList:'',
    show_attr: false,
    message: false,
    page:0,
    
    
  },
  
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this
    app.bind();
    console.log(2222222222222)
    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        page.setData({
          networkType: res.networkType
        })
      }
    });

    mta.Page.init();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // app.reseat();
    this.getDetail();
    var equipmentSystem = wx.getStorageSync('equipmentSystem');
    console.log(equipmentSystem)
    this.getCountDetail();
    this.getIcon();
    this.getInfomation();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var url = app.getcurrentPage();
    wx.setStorageSync("from_url", url);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var page = this;

    page.getDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading(); 

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading(); 

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    
   
    var url = app.getcurrentPage();
    var user_id = wx.getStorageSync('user_id');
    return {
      path: url+"?user_id=" + user_id,
      title: '得道1314'
    };

  },
  
  /**
   * 
   * 点击tabBar时触发
   * */ 

  onTabItemTap(item) {
    
  },

  // 跳转到web前获取的数据
  getInfomation: function () {
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that = this;
    wx.request({
      url: api.wechat.userInfo,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        // access_token: access_token
      },

      success: function (res) {
        console.log(res);
        
      }
    });
  },

  // 获取图标
  getIcon:function(){
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that = this;
    wx.request({
      url: api.index.menu,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {

          that.setData({
             iconList:res.data.data
          })

          console.log(that.data.iconList)

        } else if (res.data.code == -1) {

          
        }
        
      }
    });
  },

  
  
  //获取首页
  getDetail:function(){
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    var that=this;
    wx.request({
      url: api.index.index,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        page:that.data.page
      },

      success: function (res) {
       console.log(res);
       if(res.data.code == 0){
         that.setData({
           bannerList: res.data.data.banner_list,
           freeList: res.data.data.freeSectionList,
           lessonList: res.data.data.lesson_list,
           filmList:res.data.data.film_section,
           film_auth: res.data.data.film_auth,
           wendaoList: res.data.data.wendao_list
         });
         console.log(that.data.bannerList)
       } else if (res.data.code == -1){
        
         wx.navigateTo({
           url: '/pages/grant/grant',
         })
       }
       console.log(that.data.bannerList)
      }
    });
    
  },

  

  //跳转到免费专栏列表
  tofree:function(e){
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var proList = this.data.freeList;
    var name = proList[index].name;
    var lesson_id = proList[index].id;
    
    wx.navigateTo({
      url: '/pages/free/free?lesson_id=' + lesson_id,
    })
  },


  //跳转到课程专栏列表
  toCourse: function (e) {
    var page = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var lessonList = page.data.lessonList;
    var lessonId = lessonList[index].id;
    let str = JSON.stringify(lessonList[index]);
    console.log(str);
    console.log("————————————————测试————————————————");
    console.log(lessonId);
    wx.navigateTo({
      url: '../../pages/classColumn/classColumn?lessonId=' + lessonId,
    })
  },

  //跳转到专栏
  toColumnList:function(){
    wx.navigateTo({
      url: '../../pages/coulumnList/coulumnList',
    })
  },

  toFreeList:function(){
    wx.navigateTo({
      url: '../../pages/freeList/freeList',
    })
  },

  //跳转到影视
  toMoviesList: function () {
    if(this.data.film_auth == 1){
      wx.navigateTo({
        url: '../../pages/moviesList/moviesList',
      })
    }
    
  },

  //跳转到影视专栏列表
  toMovies:function(e){
    var page = this;
    console.log(e);
    var index = e.currentTarget.dataset.index;
    var filmList = page.data.filmList;
    var lessonId = filmList[index].id;
    let str = JSON.stringify(filmList[index]);
    console.log(str);
    if(page.data.film_auth == 1){
      wx.navigateTo({
        url: '../../pages/movies/movies?lessonId=' + lessonId,
      })
    }


    
  },

  // 跳转到问道专栏
  towendao:function(){
    wx.navigateTo({
      url: '../../pages/wendao/wendao',
    })
  },

  // 跳转到问道列表
  towendaoList:function(res){
    var page = this 
    var index = res.currentTarget.dataset.index;
    var lessonId = page.data.wendaoList[index].id

    wx.navigateTo({
      url: '../../pages/wendaoList/wendaoList?lessonId=' + lessonId,
    })

  },

  //影视提示框
  doMessage: function () {
    if(this.data.film_auth != 1){
      this.setData({
        show_attr: true,
        message: true,
      });
    }
  },

  //点击弹窗退出
  clear: function () {
    var page = this;
    page.setData({
      show_attr: false,
      message: false,
    })

  },

  //banner跳转
  navigatorClick: function (res){
    console.log(res);
    var page = this;
    var bannerList = page.data.bannerList;
    console.log(bannerList)
  },

  // 导航栏跳转
  toIconDetail:function(res){
     console.log(res)
     var index = res.currentTarget.dataset.index

    

  },



  //设置tabBar右上角数字
  reseat: function () {
    console.log(this.data.count)
    wx.setTabBarBadge({
      index: 2,
      text: this.data.count
    })
  },

  //移除tabBar右上角数字
  remove: function () {
    wx.removeTabBarBadge({
      index: 2
    })
  },

  //获取未读消息详情
  getCountDetail: function () {
    var page = this;
    var access_token = wx.getStorageSync('access_token');
    console.log(access_token)
    wx.request({
      url: api.message.msg,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token
      },

      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          page.setData({
            count: res.data.data.count,
            list: res.data.data.list
          })

          if (res.data.data.count == 0) {
            page.remove();
          } else {
            page.reseat();
          }


        } else if (res.data.code == 1) {



        } else if (res.data.code == -1) {
          
        }

      }
    });
  },
})